<?php

//use yii\web\View;

use yii\helpers\Url;

?>
<?php echo $this->render("/layouts/top")?>

<style>

.questiondiv{


	margin:0px auto; 
	margin-top:1px; 
	width:1000px; 
	height:28px; 
	line-height:28px; 
	background-color:#EEE;

}

.questiondiv span{

	
}

ul{margin:0;padding:0;list-style:none;}  


.rightnum{
	display:inline-block;
	padding:0px;
	width:50px; 
	height:28px; 
	font-size:18px;
	text-align:center;
}


</style>
<script type="text/javascript" src="/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="/js/NoLimitCategory.js"></script>
<div style="margin:0px auto; width:1000px; margin-bottom:10px;"><!--
--><div style="display:inline-block;">选择分类：</div><!--
--><div id="cont_category" style="display:inline-block; vertical-align:top">
  </div><!--
--><div style="display:inline-block; margin-left:10px; font-size:12px; vertical-align:top; width:65px; height:24px; padding-left:5px; background:#F0F0F0">
    <div style="display:inline-block; height:24px; line-height:24px;">显示全部</div><!--
 --><div style="display:inline-block; height:24px; line-height:24px; vertical-align:top; position:relative">
      <input id="graduated" type="checkbox"<?php if($graduated == 0)echo ' checked="checked"'?> style="position:absolute; top:6px" />
    </div><!--
--></div><!--

--><div style="display:inline-block; margin-left:5px;"><input type="button" value="查找" onclick="search()" style=" width:50px; height:24px;"/></div>
</div>
<?php

//pre($knowledge);
	for($i=0; $i<count($knowledge); $i++){
		
		$cname1 = "[".$knowledge[$i]["tname"]."]";
		$cname2 = empty($knowledge[$i]["cname2"]) ? "" : "[".$knowledge[$i]["cname2"]."]";
		$cname3 = empty($knowledge[$i]["cname3"]) ? "" : "[".$knowledge[$i]["cname3"]."]";
		$cname4 = empty($knowledge[$i]["cname4"]) ? "" : "[".$knowledge[$i]["cname4"]."]";
		$cname5 = empty($knowledge[$i]["cname5"]) ? "" : "[".$knowledge[$i]["cname5"]."]";
		
		$category = $cname1.$cname2.$cname3.$cname4.$cname5;
		//echo $category;
		
		$right = $knowledge[$i]["right"];

        $color = \common\components\Common::getRightColor($right);
		
		$curtime = time();
		
		$last_right_time = $knowledge[$i]["last_right_time"];

        $locktime = \common\components\Common::getLocktime($right);
		
		if($last_right_time+$locktime > $curtime){
			$questioncolor = '#BBB';
		}else{
			$questioncolor = '#000';
		}

?>

<div class="questiondiv">
  <span style="display:inline-block; width:50px; text-align:center"><?php echo $knowledge[$i]["id"]?></span>  
  
  <span style="display:inline-block; width:820px; color:<?php echo $questioncolor?>">
  <?php echo $category?> <?php echo $knowledge[$i]["question"]?>
  </span>
  <span class="rightnum"><strong style="color:<?php echo $color?>;"><?php echo $right?></strong></span>  
  <span style="display:inline-block; width:50px; text-align:center;"><input type="button" value="答案" onclick="seeanswer(this)" /></span>  
</div>

<div style="margin:0px auto; width:1000px;display:none">
  <div style="padding:4px;">
  <?php echo stripslashes($knowledge[$i]["answer"])?>
  </div>
  <div style="margin:0px auto; width:100%; text-align:center; padding:4px; padding-bottom:8px;">
      <input type="button" value="收起答案" onclick="closeanswer(this)"/>
      <input type="button" value="编辑知识" onclick="edit(<?php echo $knowledge[$i]["id"]?>)"/>
      <input type="button" value="删除此题" onclick="remove(<?php echo $knowledge[$i]["id"]?>)"/>
      <input type="button" value="回答错误" onclick="wrong(<?php echo $knowledge[$i]["id"]?>)"/>
      <input type="button" value="回答正确" onclick="right(<?php echo $knowledge[$i]["id"]?>)"/>
  </div>
</div>
<?php
	}
?>

<br>

<?php
	$pageurl = "?c1=$c1&c2=$c2&c3=$c3&cid=$cid&graduated=$graduated&page=";
	
?>
<div style=" margin:0px auto; width:1000px; height:28px; line-height:28px; background-color:#EEE; text-align:center">
<span>总数据:<?php echo $pv->dataCount?>条</span>
<a href="<?php echo $pageurl."1"?>">首页</a>
<a href="<?php echo $pageurl.$pv->prevPage?>">上一页</a>
<span><?php echo $pv->currentPage."/".$pv->pageCount?></span>
<a href="<?php echo $pageurl.$pv->nextPage?>">下一页</a>
<a href="<?php echo $pageurl.$pv->pageCount?>">末页</a>
</div>

<script language="javascript">

var page = <?php echo $pv->currentPage;?>;
var c1 = <?php echo $c1?>;
var c2 = <?php echo $c2?>;
var c3 = <?php echo $c3?>;

var nlc = new NoLimitCategory(<?php echo json_encode($treeData, JSON_UNESCAPED_UNICODE)?>, document.getElementById("cont_category"), "全部");

nlc.onSelectCreate = function(selectObj){
	selectObj.style.cssText = 'margin-left:5px;height:24px;';
};


var cids = [];
if(c1 != 0)cids.push(c1);
if(c2 != 0)cids.push(c2);
if(c3 != 0)cids.push(c3);


nlc.append(cids);




function search(){
	var categorys = nlc.getSelects();
	var graduated = ($('#graduated').is(':checked')) ? 0 : 1;//0显示全部，1只显示未毕业的
	//var pageurl = "?tid="+typeid+"&page="+page;
	var c1=0, c2=0, c3=0, cid = 0;
	for(var i=0; i<categorys.length; i++){
		if(i == 0)c1 = categorys[i].value;
		if(i == 1)c2 = categorys[i].value;
		if(i == 2)c3 = categorys[i].value;
		if(categorys[i].value > 0)cid = categorys[i].value;//所属分类为不为0的最后一个
	}

	var pageurl = "?"+"c1="+c1+"&c2="+c2+"&c3="+c3+"&cid="+cid+"&graduated="+graduated+"&page="+page;

	window.location.href = pageurl;

}

function seeanswer(obj){

	var div = $(obj).parent().parent().next();

	var h = div.height();

	div.css('display', 'block');
	div.css('height', '0px');
	
	div.animate({height:h}, 500);

}

function closeanswer(obj){
	var div = $(obj).parent().parent();
	var h = div.height();
	
	div.animate({height:'0px'}, 500, function(){
		div.css('display', 'none');
		div.height(h);//重新给它高度，否则下次展开的时候不灵，因为展开的时候这个高度需要读出来
	});
}

function edit(kid){
	window.location.href = "<?php echo Url::to(['edit'])?>"+"?id="+kid;
}

function right(kid){

	new Ajax().post({
		url : "<?php echo Url::to(['async-right'])?>",
		data : "id="+kid,
		resultType : 'json',
		onLoaded : function(result){

			if(result.code == 1){
				alert("成功");
				window.location.reload();
			}else{
				alert(result.info);
			}
		}
						
	});
}

function wrong(qid){
    $.ajax({
        url : "<?php echo Url::to(['async-wrong'])?>",
        data : {"id" : qid},
        dataType : 'json',
        success : function (data) {
            if(data.code == 200){
                alert("成功");
                window.location.reload();
            }else{
                alert(data.info);
            }
        }
    });
}

function remove(nid) {
    console.log(nid);

    if(confirm("确定删除？")){
        $.ajax({
            url : "<?php echo Url::to(["async-remove"])?>"+"?id="+nid,
            type : 'GET',
            dataType : 'json',
            success : function (data) {
                console.log(data);
                if(data.code == 200){
                    alert("删除成功");
                    window.location.reload();
                }else{
                    alert(data.info);
                }
            }
        });
    }

}


</script>
<br>
<?php echo $this->render("/layouts/footer")?>
